package com.mijie.homi.common.util;

import java.util.Date;

import net.sf.json.JSONObject;

import com.mijie.core.ApplicationProperties;
import com.mijie.homi.common.constant.CategoryCode;
import com.mijie.homi.common.constant.TypeCode;
import com.mijie.homi.common.domain.PushMessage;
import com.mijie.homi.common.domain.chat.ChatMessage;
import com.mijie.homi.common.domain.user.User;
import com.mijie.homi.common.domain.user.UserExtInfo;
import com.mijie.homi.common.protocol.HomiProtocol;
import com.mijie.util.Helper;

public class HomiUtil {

	public static String genBothUserUUID(int userId,int contactId){
		StringBuilder uuid =new StringBuilder();
		if(userId<contactId){
			uuid.append(userId).append("_").append(contactId);
		}else{
			uuid.append(contactId).append("_").append(userId);
		}
		return uuid.toString();
	}
	
	public static String getMoodResponderTableName(long moodId){
		return "moodResponder_1";
	}
	
	public static String getTopicConfusedTableName(int topicId){
		return "topicConfused_1";
	}
	
	public static String getFriendTableName(int userId){
		return "friend_1";
	}
	
	public static String getTopicInTypeTableName(int type){
		return "topicInType_"+type;
	}
	
	public static String getTimelineTableName(int userId){
		return "timeline_1";
	}
	
	private static int[] doOfflinePushAtType = {TypeCode.SYSTEM_MESSAGE,TypeCode.FRIEND_REQUEST,TypeCode.PRIVACY_APPLY,TypeCode.NEW_TOPIC,
			TypeCode.AUDIT,TypeCode.TOPIC_OPINION,TypeCode.PROSECUTE,TypeCode.REPLY,TypeCode.COMMENT,TypeCode.MOOD_VIEWAPPLY};
	
	public static boolean doOfflinePush(int notifyType){
		for(int t : doOfflinePushAtType){
			if(t==notifyType) return true;
		}
		return false;
	}
	
	public static String getRealUrl(String res){
		if(res==null || res.isEmpty())
			return "";
		return ApplicationProperties.get("resource.url")+res;
	}
	
	/**
	 *  正面心情：开心1、狂喜5
		负面心情：伤心3、郁闷4、纠结7、愤怒2、委屈8
		中性心情：无聊6、无感9
	 */
	public static String getMoodClan(int mood){
		if(mood==1 || mood==5) return "1,5";
		if(mood==2 || mood==3 || mood==4 || mood==7 ||  mood==8) return "2,3,4,7,8";
		if(mood==6 || mood==9) return "6,9";
		return null;
	}
	
	public static ChatMessage makeChatMessage(JSONObject json,User userOfSender,UserExtInfo extInfoOfSender,UserExtInfo extInfoOfReceiver){
		ChatMessage cm = new ChatMessage();
		cm.setId(Helper.genUUID());
		if(json.containsKey("length"))
			cm.setLength(json.getInt("length"));
		cm.setCategory((short)json.getInt("category"));
		cm.setSender(extInfoOfSender.getId());
		cm.setReceiver(extInfoOfReceiver.getId());
		cm.setCreateAt(new Date());
		if(json.getInt("category")==CategoryCode.CHAT_VOICE)
			cm.setMessage(ApplicationProperties.get("chat.voice.home")+json.getString("message"));
		else cm.setMessage(json.getString("message"));
		
		
		if(json.getInt("category")==CategoryCode.CHAT_GIF)
			cm.setHint(json.getString("hint"));
		
		cm.setMood(extInfoOfSender.getMood());
		cm.setNickName(userOfSender.getNickName());
		cm.setAvatar(userOfSender.getAvatar());
		cm.setGender(userOfSender.getGender());
		cm.setLastLoginAt(extInfoOfSender.getLastLoginAt());
		cm.setLatitude(extInfoOfSender.getLatitude());
		cm.setLongitude(extInfoOfSender.getLongitude());
		cm.setMoodUpdateAt(extInfoOfSender.getMoodUpdateAt());
		String image = json.getString("image");
		cm.setLargeImage(image.isEmpty()?"":ApplicationProperties.get("chatimg.large.home")+image);
		cm.setSmallImage(image.isEmpty()?"":ApplicationProperties.get("chatimg.small.home")+image);
		return cm;
	}
	
	public static HomiProtocol makeChatMessageProtocol(ChatMessage message,boolean isFreind){
		HomiProtocol pack = new HomiProtocol(TypeCode.CHAT);
		JSONObject msg = JSONObject.fromObject(message);
		
		if(message.getCategory()==CategoryCode.CHAT_VOICE){
			msg.put("message", HomiUtil.getRealUrl(message.getMessage()));
		}
		
		msg.put("isFriend", isFreind);
		msg.put("avatar", HomiUtil.getRealUrl(message.getAvatar()));
		msg.put("smallImage", HomiUtil.getRealUrl(message.getSmallImage()));
		msg.put("largeImage", HomiUtil.getRealUrl(message.getLargeImage()));
		msg.put("createAt", message.getCreateAt().getTime());
		msg.put("lastLoginAt", message.getLastLoginAt().getTime());
		msg.put("moodUpdateAt", message.getMoodUpdateAt().getTime());
		pack.setContent(msg.toString());
		return pack;
	}
	
	public static PushMessage makeChatPushMessage(ChatMessage message,User sender){
		PushMessage pushMsg = new PushMessage();
		pushMsg.setId(Helper.genUUID());
		pushMsg.setIdx(System.currentTimeMillis());
		pushMsg.setDestinationDeviceId("");
		pushMsg.setReceiver(message.getReceiver());
		pushMsg.setContentType(TypeCode.CHAT);
		StringBuilder pushContent = new StringBuilder();
		pushContent.append(sender.getNickName()).append(":");
		if(message.getCategory()==CategoryCode.CHAT_VOICE){
			pushContent.append("[语音]");
		}else if(message.getCategory()==CategoryCode.CHAT_TEXT_MESSAGE){
			if(!message.getMessage().trim().isEmpty()){
				if(message.getMessage().startsWith("[location]")){
					pushContent.append("[地理位置]");
				}else{
					pushContent.append(message.getMessage());
				}
			}else{
				if(!message.getSmallImage().trim().isEmpty()){
					pushContent.append("[图片]");
				}
			}
		}else if(message.getCategory()==CategoryCode.CHAT_GIF){
			pushContent.append("[动态表情]");
		}
		pushMsg.setContent(pushContent.toString());
		return pushMsg;
	}
}
